package com.fsnbigdata.service.impl;
import com.fsnbigdata.service.api.DistributionService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.mapping.Map;
import org.springframework.roo.addon.layers.service.annotations.RooServiceImpl;
import com.fsnbigdata.domain.Distribution;
import com.fsnbigdata.domain.Organism;
import com.fsnbigdata.repository.DistributionRepository;
import io.springlets.data.domain.GlobalSearch;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * = DistributionServiceImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooServiceImpl(service = DistributionService.class)
@Service
@Transactional(readOnly = true)
public class DistributionServiceImpl implements DistributionService {

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    private DistributionRepository distributionRepository;

    /**
     * TODO Auto-generated constructor documentation
     *
     * @param distributionRepository
     */
    @Autowired
    public DistributionServiceImpl(DistributionRepository distributionRepository) {
        setDistributionRepository(distributionRepository);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return DistributionRepository
     */
    public DistributionRepository getDistributionRepository() {
        return distributionRepository;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param distributionRepository
     */
    public void setDistributionRepository(DistributionRepository distributionRepository) {
        this.distributionRepository = distributionRepository;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param entity
     * @return Distribution
     */
    @Override
    @Transactional
    public Distribution save(Distribution entity) {
        return getDistributionRepository().save(entity);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     * @return Distribution
     */
    @Override
    public Distribution findOne(Long id) {
        return getDistributionRepository().findOne(id);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     * @return Distribution
     */
    @Override
    public Distribution findOneForUpdate(Long id) {
        return getDistributionRepository().findOneDetached(id);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     * @return List
     */
    @Override
    public List<Distribution> findAll(Iterable<Long> ids) {
        return getDistributionRepository().findAll(ids);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param organism
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    @Override
    public Page<Distribution> findByOrganism(Organism organism, GlobalSearch globalSearch, Pageable pageable) {
        return getDistributionRepository().findByOrganism(organism, globalSearch, pageable);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param organism
     * @return Long
     */
    @Override
    public long countByOrganism(Organism organism) {
        return getDistributionRepository().countByOrganism(organism);
    }
    
    /**
     * 查询对应省下面的市
     * @param province
     * @return
     */
    @Override
    public List<String> findAllByProvince(String province) {
        return getDistributionRepository().findAllByProvinceLike(province);
    }

    /**
     * 查询市下面的县和区
     * @param city
     * @return
     */
    public List<String> findAllByCity(String city) {
        return getDistributionRepository().findAllByCityLike( city );
    }

    /**
     * 查询先和区下面的详细地址
     * @param county
     * @return
     */
    public List<String> findAllByCounty(String county) {
        return getDistributionRepository().findAllByCountyLike( county );
    }

//    /**
//     * 查询对应市下面的区
//     * @param city
//     * @return
//     */
//    @Override
//    public List<Distribution> findAllByCityLike(String city) {
//        return getDistributionRepository().findAllByCityLike(city);
//    }
    
    
    /**
     * 查询物种id
     * @param city
     * @param county
     * @return
     */
    @Override
    public List<Long> findByCityAndCounty(String city, String county) {
        return getDistributionRepository().findByCityAndCounty(city,county);
    }

    @Override
    public List<BigInteger> geOrganismIdPronvince() {
        return getDistributionRepository().geOrganismIdPronvince();
    }

    @Override
    public List<String> query1(Distribution distribution) {
        return getDistributionRepository().queryProvince(distribution.getProvince());
    }

    @Override
    public List<String> query2(Distribution distribution) {
        return getDistributionRepository().queryCity( distribution.getProvince(),distribution.getCity());
    }

    @Override
    public List<Long> query3(Distribution distribution) {
        return getDistributionRepository().queryCounty( distribution.getProvince(),distribution.getCity(),distribution.getCounty());
    }


    /**
     * TODO Auto-generated method documentation
     *
     * @return Class
     */
    @Override
    public Class<Distribution> getEntityType() {
        return Distribution.class;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Class
     */
    @Override
    public Class<Long> getIdType() {
        return Long.class;
    }
}
